package com.tjyx.maintain.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.tjyx.common.util.db.DBConnector;
import com.tjyx.maintain.dto.ProductClass;

public class ProductClassDao {
private Connection connection;
	
	public ProductClassDao(){
		connection=DBConnector.getConnection();
	}
	
    public void addProductClass(int sortNumber,int parentId ,String productClassName) throws SQLException{
    	String sql="insert into eps_productclass(sortNumber,parentId,productClassName) values(?,?,?)";
    	PreparedStatement statement=connection.prepareStatement(sql);
    	statement.setInt(1,sortNumber);
    	statement.setInt(2,parentId);
    	statement.setString(3,productClassName);
    	statement.execute();
    }	
    
    public void deleteProductClass(int id) throws SQLException{
    	Statement statement=connection.createStatement();
    	statement.executeUpdate(" delete  from eps_productclass where productClassId='"+id+"'");
    }	
    
    public List<ProductClass> queryProductClass() throws SQLException{
    	Statement statement=connection.createStatement();
    	ResultSet rs=statement.executeQuery("select * from eps_productclass");
    	 List<ProductClass> list=new  ArrayList<ProductClass>();
    	while(rs.next()){
    		int id=rs.getInt(1);
    		String name=rs.getString(2);
    		ProductClass productClass=new ProductClass();
    		productClass.setProductClassId(rs.getInt(1));
    		productClass.setSortNumber(rs.getInt(2));
    		productClass.setParentId(rs.getInt(3));
    		productClass.setProductClassName(rs.getString(4));
    		list.add(productClass);
    	}
    	return list;
    }

	public void updateProductClass(Integer id, String columnName,
			String columnValue) throws SQLException {
		String sql="update eps_productclass set "+columnName+"=?  where productClassId="+"?";
		PreparedStatement statement=connection.prepareStatement(sql);
		statement.setString(1, columnValue);
		statement.setInt(2, id);
		statement.execute();
	}	
}
